package com.sbtr.business.trainingserver.balancer.impl;

import com.sbtr.business.trainingserver.balancer.ITraningServerBalancer;
import com.sbtr.business.trainingserver.service.ITrainingServerHttpApi;
import com.sbtr.business.trainingserver.service.impl.TrainingServerServiceImpl;
import com.sbtr.open.trainingserver.dto.TrainingServerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Iterator;
import java.util.List;

/**
 * 最小等待时间负载均衡器，这个也是默认的负载均衡器
 * 
 * @author oujh5
 *
 */

@Component("minimumWaitTimeBalancer")
@ConditionalOnExpression("'${training-server.balancer-class}'.equals('MinimumWaitTimeBalancer')")
public class MinimumWaitTimeBalancer implements ITraningServerBalancer {

	@Autowired
	private ITrainingServerHttpApi serverHttpApi;
	@Autowired
	private TrainingServerServiceImpl serverService;

	@Override
	public TrainingServerDto choose() {

		// 查询所有在线的GPU服务器
		List<TrainingServerDto> serverDtos = serverService.getList(TrainingServerDto.TRAINING_SERVER_STATUS_ONLINE,
				TrainingServerDto.SERVER_TYPE_TRAINING, null);
		// 实时检测服务器是否在线,把不在线的排除
		if (serverDtos != null && !serverDtos.isEmpty()) {
			Iterator<TrainingServerDto> iterator = serverDtos.iterator();
			while (iterator.hasNext()) {
				TrainingServerDto next = iterator.next();
				if (!serverHttpApi.serverIsOnline(next)) {
					iterator.remove();
				}
			}
		}
		Assert.isTrue(serverDtos != null && !serverDtos.isEmpty(), "没有可用的gpu服务器");

		// 轮询获取查询事件最短的一个
		TrainingServerDto resultDto = null;
		Long minimumWaitTime = Long.MAX_VALUE;
		for (TrainingServerDto serverDto : serverDtos) {
			long waitTimeTemp = serverHttpApi.getWaitTime(serverDto);
			if (waitTimeTemp < minimumWaitTime) {
				resultDto = serverDto;
				minimumWaitTime = waitTimeTemp;
			}
		}

		return resultDto;
	}

	@Override
	public TrainingServerDto chooseMachine() {
		// 查询所有在线的GPU服务器
		List<TrainingServerDto> serverDtos = serverService.getList(TrainingServerDto.TRAINING_SERVER_STATUS_ONLINE,
				TrainingServerDto.SERVER_TYPE_DATA_TRAINING, null);
		// 实时检测服务器是否在线,把不在线的排除
		if (serverDtos != null && !serverDtos.isEmpty()) {
			Iterator<TrainingServerDto> iterator = serverDtos.iterator();
			while (iterator.hasNext()) {
				TrainingServerDto next = iterator.next();
				if (!serverHttpApi.serverIsOnline(next)) {
					iterator.remove();
				}
			}
		}
		Assert.isTrue(serverDtos != null && !serverDtos.isEmpty(), "没有可用的gpu服务器");

		// 轮询获取查询事件最短的一个
		TrainingServerDto resultDto = null;
		// Long minimumWaitTime = Long.MAX_VALUE;
		Integer mintaskNum = Integer.MAX_VALUE;
		double percent = Double.MAX_VALUE;
		for (TrainingServerDto serverDto : serverDtos) {
			String machineStatus = serverHttpApi.getMachineStatus(serverDto);

			Integer taskNum = Integer.parseInt(machineStatus.split(",")[0]);
			double cpuInfo = Double.parseDouble(machineStatus.split(",")[1]);
			// System.out.println("dd");
			if (taskNum <= mintaskNum) {
				if (cpuInfo <= percent) {
					resultDto = serverDto;
					mintaskNum = taskNum;
					percent = cpuInfo;
				}
			}
		}

		return resultDto;
	}

}
